import wepy from 'wepy'
var config = require('./config.js')
var util = require('./utils.js')


module.exports = {
    getUserInfo: getUserInfo,
    appLogin: appLogin
}

function appLogin(obj, cb){
    wepy.setStorageSync('isGettingUser', true);
    var userInfo = wepy.getStorageSync(util.Suan.encode('appUserInfo')) ?
        JSON.parse(util.Suan.decode(wepy.getStorageSync(util.Suan.encode('appUserInfo')))) :
        '';
    obj = obj ? obj : []
    getUserInfo(obj, cb)
}

function getUserInfo(obj, cb) {
    wepy.removeStorageSync('appUserInfo')
    wepy.removeStorageSync('refuseGetUserinfo')
    wepy.login({
        success: function(loginres) {
            if (loginres.code) {
                wepy.getUserInfo({
                    success: function(res) {
                        res.rawData = JSON.parse(res.rawData);
                        wepy.request({
                            url: config.API_SERVER + '/wx/openid',
                            method: 'post',
                            data: {
                                code: loginres.code
                            },
                            success: function(data) {
                                res.rawData.openid = data.data.openid
                                wepy.setStorageSync('appUserInfo', JSON.stringify(res.rawData))
                                wepy.setStorageSync('refuseGetUserinfo', 'suc')
                                wepy.request({
                                    url: config.API_SERVER + '/wx/user',
                                    method: 'post',
                                    data: res.rawData,
                                    complete: function(res) {
                                        wx.getLocation({
                                            type: 'wgs84',
                                            success(res) {
                                                wepy.setStorageSync('latitude', res.latitude)
                                                wepy.setStorageSync('longitude', res.longitude)
                                            },
                                            complete() {
                                                cb ? cb(null, res.rawData) : '';
                                            }
                                        })
                                    }
                                });
                            },
                            fail: function(err) {
                                wepy.setStorageSync('isGettingUser', false);
                                wepy.hideLoading();
                                cb ? cb('err', '获取用户微信信息失败！') : '';
                                wepy.setStorageSync('refuseGetUserinfo', 'err')
                            }
                        });
                    },
                    fail: function(res){
                        wepy.setStorageSync('isGettingUser', false);
                        wepy.hideLoading();
                        cb ? cb('err', '获取用户微信信息失败！') : '';
                        wepy.setStorageSync('refuseGetUserinfo', 'err')
                    }
                })
            } else {
                wepy.setStorageSync('isGettingUser', false);
                wepy.hideLoading();
                cb ? cb('err', '获取微信信息失败！') : '';
            }
        },
        fail: function(res) {
            wepy.setStorageSync('isGettingUser', false);
            wepy.hideLoading();
            cb ? cb('err', '微信授权登录失败！') : '';
        }
    });
}

